/**
 * Created by shc@lierda.com on 2017/2/22.
 */
var React = require('react');
var Icon = require('../components/Icon');
var ajax = require('../tools/ajax');

module.exports = React.createClass({
    getInitialState: function (){
        return {
            theme:"Default",
            icon:"#",
            flag:false,//判断是否为首页
            cartNum:0,//加入购物车商品数量
            shopInfo:[],
            searchFlag:false,//是否为分类页
        }
    },

    componentDidMount: function () {
        setTimeout(this.attr,100);
        if(this.props.actionType != "ewm"){
            window.goBack = this.goBack;
        }else{
            //window.jsbridge.ewm();
        }
        this.fleshNum();
    },

    attr:function(){
        if(this.props.theme){
            this.setState({theme:this.props.theme});
        }
        if(this.props.icon){
            this.setState({icon:this.state.icon});
        }
        if(this.props.flag){
            this.setState({flag:this.state.flag});
        }
        if(this.props.country){
            this.setState({country:this.props.country});
        }
    },
    /**调用二维码方法*/
    ewm(){
        if(window.jsbridge){
            window.jsbridge.ewm();
            window.ewmCallBack = function(e){
                localStorage.setItem("ewm",e);
                location.href = "#qrcodePage";
            };
        }
    },
    /**安卓返回键功能*/
    goBack(){
        location.href = this.props.actLink;
    },
    cart(){
        var userId = localStorage.getItem("userId");
        if(userId){
            location.href = "#cart";
        }else{
            location.href = "#login";
        }
    },
    /**购物车数量*/
    fleshNum(){
        var cartNum= localStorage.getItem("cartNum");
        if(cartNum){
            this.setState({cartNum:cartNum});
        }
    },
    action(e){
        var action = e.currentTarget.getAttribute("action");
        location.href = action;
    },
    render: function () {
        var p = this;
        var cartNum = p.state.cartNum;
        var shopInfo = p.state.shopInfo;
        var country = localStorage.getItem("country");
        if(!country){
            country = "杭州";
        }
        return (
            <div className={p.state.theme+"_Header" + "  " + "head"}>
                <div className="head-l" onClick={p.props.actionType == "ewm"?p.ewm:p.goBack}>
                     <Icon icon={p.props.icon} style={{fontSize:"1.05rem"}}/>
                </div>
                <div className="h-cont" style={{display: p.props.flag?"inline-block":"none"}}>
                    <div className="u-head-logo">
                        <span style={{fontWeight:"bold"}}>宠 物 之 家</span>
                    </div>
                </div>
                <div className="h_search" style={{display:p.props.searchFlag?"block":"none"}}>
                    <Icon icon="#icon-search" />
                    <input ref="inputText" type="text" placeholder="可输入商品名、宠物名、品牌名"/>
                </div>
                <div className="head-f" onClick={p.cart} style={{display:p.props.flag?"block":"none"}}>
                    <span className="span-w">{cartNum?cartNum:0}</span>
                    <Icon icon="#icon-gouwuche" style={{fontSize:"1.05rem"}}/>
                </div>
            </div>
        )
    }
});
